package com.pudu.mydemo.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.BatteryManager;
import android.os.IBinder;
import android.util.Log;


/**
 * Created by guiliu on 2016/8/27.
 */
public class BatteryService extends Service {

    private static final String TAG = "BatteryReceiver";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate--------------");
        IntentFilter batteryfilter = new IntentFilter();
        batteryfilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        batteryfilter.addAction(Intent.ACTION_BATTERY_LOW);
        batteryfilter.addAction(Intent.ACTION_BATTERY_OKAY);
        registerReceiver(batteryReceiver, batteryfilter);
//        registerReceiver()
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand--------------");
        return Service.START_STICKY; //
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy--------------");
        super.onDestroy();
        this.unregisterReceiver(batteryReceiver);

    }

    private BatteryChangedReceiver  batteryReceiver = new BatteryChangedReceiver();

    /*
    *BatteryChangedReceiver BATTERY_CHANGED_ACTION---
    *健康状态 BATTERY_HEALTH_GOOD
    *当前使用USB电源 BATTERY_PLUGGED_USB
    *正在充电 BATTERY_STATUS_CHARGING
    *电压voltage = 3858 technology = Li-poly temperature = 368 level = 56 scale = 100 （56%电量）
    */
    class BatteryChangedReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            final String action = intent.getAction();
            if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)) {
                System.out
                        .println("BatteryChangedReceiver BATTERY_CHANGED_ACTION---");
                // 当前电池的电压
                int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,
                        -1);
                // 电池的健康状态
                int health = intent
                        .getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
                switch (health) {
                    case BatteryManager.BATTERY_HEALTH_COLD:
                        System.out.println("BATTERY_HEALTH_COLD");
                        break;
                    case BatteryManager.BATTERY_HEALTH_DEAD:
                        System.out.println("BATTERY_HEALTH_DEAD ");
                        break;
                    case BatteryManager.BATTERY_HEALTH_GOOD:
                        System.out.println("BATTERY_HEALTH_GOOD");
                        break;
                    case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                        System.out.println("BATTERY_HEALTH_OVERHEAT");
                        break;
                    case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                        System.out.println("BATTERY_HEALTH_COLD");
                        break;
                    case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                        System.out.println("BATTERY_HEALTH_UNKNOWN");
                        break;
                    case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                        System.out.println("BATTERY_HEALTH_UNSPECIFIED_FAILURE");
                        break;
                    default:
                        break;
                }
                // 电池当前的电量, 它介于0和 EXTRA_SCALE之间
                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                // 电池电量的最大值
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                // 当前手机使用的是哪里的电源
                int pluged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,
                        -1);
                switch (pluged) {
                    case BatteryManager.BATTERY_PLUGGED_AC:
                        // 电源是AC charger.[应该是指充电器]
                        System.out.println("BATTERY_PLUGGED_AC");
                        break;
                    case BatteryManager.BATTERY_PLUGGED_USB:
                        // 电源是USB port
                        System.out.println("BATTERY_PLUGGED_USB ");
                        break;
                    default:
                        break;
                }
                int status = intent
                        .getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                switch (status) {
                    case BatteryManager.BATTERY_STATUS_CHARGING:
                        // 正在充电
                        System.out.println("BATTERY_STATUS_CHARGING ");
                        break;
                    case BatteryManager.BATTERY_STATUS_DISCHARGING:
                        System.out.println("BATTERY_STATUS_DISCHARGING  ");
                        break;
                    case BatteryManager.BATTERY_STATUS_FULL:
                        // 充满
                        System.out.println("BATTERY_STATUS_FULL ");
                        break;
                    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                        // 没有充电
                        System.out.println("BATTERY_STATUS_NOT_CHARGING ");
                        break;
                    case BatteryManager.BATTERY_STATUS_UNKNOWN:
                        // 未知状态
                        System.out.println("BATTERY_STATUS_UNKNOWN ");
                        break;
                    default:
                        break;
                }
                // 电池使用的技术。比如，对于锂电池是Li-ion
                String technology = intent
                        .getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
                // 当前电池的温度
                int temperature = intent.getIntExtra(
                        BatteryManager.EXTRA_TEMPERATURE, -1);
                System.out.println("voltage = " + voltage + " technology = "
                        + technology + " temperature = " + temperature
                        + " level = " + level + " scale = " + scale);
                if(level == 45) {
                    launcherActivity();
                }

            } else if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)) {
                // 表示当前电池电量低
                Log.e("barrery","BatteryChangedReceiver ACTION_BATTERY_LOW");
                System.out
                        .println("BatteryChangedReceiver ACTION_BATTERY_LOW---");
            } else if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_OKAY)) {
                // 表示当前电池已经从电量低恢复为正常
                Log.e("barrery","BatteryChangedReceiver ACTION_BATTERY_OKAY");
                System.out
                        .println("BatteryChangedReceiver ACTION_BATTERY_OKAY---");
            }
        }

        public void launcherActivity() {
            //启动电量动画
            Intent lowIntent = new Intent();
            lowIntent.setAction("android.intent.battery.low");
            lowIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(lowIntent);
        }

    }

}